<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午5:25
 */
class Login extends MY_Controller
{
    public function index()
    {

        $this->load->view('admin/login/login');
    }


    public function do_login()
    {
        $username = trim($this->input->get_post('username'));
        $password = trim($this->input->get_post('password'));


        if (empty($username) || empty($password))
        {
            $this->json_output(array(), 0, '参数错误,用户名或密码为空');
        }


        $this->load->model('staff/Model_staff', 's');

        $item = $this->s->login($username, $password);
        if (200 != $item['status'])
        {
            $this->json_output(array(), $item['status'], $item['msg']);
        }


        $item = $item['data'];
        $this->load->helper('cookie');
        set_cookie('auth', auth_code("{$item['sid']}\t{$item['login_name']}\t{$item['password']}", 'ENCODE'), 36000);


        $this->uInfo = $item;


        $this->load->model('user/Model_user_login_log', 'ull');
        $this->ull->save_log($item['sid'], $item['login_name'], USER_LOGIN_SOURCE_OTHER, USER_TYPE_USER);

        //权限初始化
        $this->load->model("staff/Model_rbac", "rbac");
        $this->rbac->set_rbac($this->uInfo['sid']);

        $this->json_outputs($item);
    }


    /**
     * 退出登陆
     */
    public function login_out()
    {
        $this->load->helper('cookie');
        set_cookie('auth', '', 0);


        $this->load->helper('url');
        redirect(url('admin').'Login/index');
    }


    /**
     * json输出
     *
     * @param array $data
     * @param int $code
     * @param $msg
     */
    //*
    private function json_outputs(array $data, $code = 200, $msg = '成功')
    {
        $r = array
        (
            'status' => $code,
            'msg'    => $msg,
            'data'   => $data,
        );


        echo json_encode($r);
        exit;
    }
    //*/
}